package com.example.demo5datajpa.domain;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
 * @author : zhuyanpeng.zhuyanpeng
 * @Description: :
 * @date :Create in  2019/6/20- 1:36
 * @Version: V1.0
 * @Modified By:
 **/
@Data
@NoArgsConstructor
public class UserInfo extends  User implements Serializable {

     String deptName;
     @Transient
     List<Dept> depts;

    public UserInfo(Integer id, String name, Integer age, String deptName) {
        super.setId(id);
        super.setName(name);
        super.setAge(age);
        this.deptName = deptName;
    }

    public UserInfo(Integer id, String name,  String deptName) {
        super.setId(id);
        super.setName(name);
        this.deptName = deptName;
    }

    @ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
    @JoinTable(name="user_dept",//中间表名字
            joinColumns=@JoinColumn(name="user_id",referencedColumnName="id"),//这行配置本表（user_dept）与中间表对应关系
            inverseJoinColumns=@JoinColumn(name="dept_id",referencedColumnName="id"))//这行配置 中间表另一字段与对应表关联关系
    //所有JoinColumn 配置 name都是中间表的字段 referencedColumnName都是对应表的字段 并非实体属性
    public List<Dept> getDepts() {
        return depts;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id='" + super.getId() + '\'' +
                "name='" + super.getName() + '\'' +
                "deptName='" + deptName + '\'' +
                '}';
    }
}
